<template>
  <div class="file-transfer-dialog">
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :before-close="handleClose"
      :destroy-on-close="!dialogVisible"
      custom-class="crud-dialog medium"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <div class="flex-1 auto dialog-content">
        <avue-form ref="addForm" class="sandbox-group-form" :option="formOption" v-model="form" @submit="handleSubmit"></avue-form>
      </div>
      <div class="flex flex-center dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button
          type="primary"
          v-if="isDisabled === false"
          @click="
            $refs.addForm.submit()
            type = 'SAVE'
          "
          >暂存
        </el-button>
        <el-button
          type="primary"
          v-if="isDisabled === false"
          @click="
            $refs.addForm.submit()
            type = 'SUBMIT'
          "
          >发送
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { formOption } from './index.option'
import { addTextMsg, detailTextMessage, updateReply } from '@/api/shipsms'
import { scrollToEl } from '@/utils/util'

export default {
  props: {
    shipList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      id: '',
      title: '',
      isDisabled: false,
      dialogVisible: false,
      form: {},
      submitLoad: false,
      savaLoad: false,
      type: ''
    }
  },
  created() {},
  mounted() {},
  computed: {
    formOption() {
      return formOption.call(this)
    },
    tenantCatShipAll() {
      let arr = [...this.shipList]
      arr = arr.map((item) => {
        return item.shipId
      })
      return arr
    },
    tenantCatShip() {
      let arr = [...this.shipList]
      if (arr.length > 1) {
        arr.unshift({
          shipId: 'all',
          shipName: '全部'
        })
      }
      return arr
    }
  },
  methods: {
    open() {
      // 发送
      this.title = '发送短报文'
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.addForm.clearValidate()
      })
      this.isDisabled = false
      this.id = ''
    },
    initForm() {
      this.form = this.$options.data().addForm
    },
    view(row) {
      this.title = '详情'
      if (row.id) {
        this.id = row.id
        // this.form = row
        this.getFormDetail()
      } else {
        this.id = ''
      }
      if (row.sendStatus != 0) {
        this.isDisabled = true
      } else {
        this.isDisabled = false
      }
      this.$nextTick(() => {
        this.$refs.addForm.clearValidate()
      })
      this.dialogVisible = true
    },
    getFormDetail() {
      let shipIds = []
      detailTextMessage(this.id).then((res) => {
        this.form = res.result
        this.form.receiverList.forEach((val) => {
          shipIds.push(val.shipId)
        })
        this.form.shipIds = shipIds
      })
    },
    handleClose() {
      this.initForm()
      this.dialogVisible = false
    },
    handleSubmit(form, done) {
      let receiverDtoList = []
      let data = { ...this.form }
      this.shipList.forEach((i) => {
        data.shipIds.forEach((t) => {
          if (i.shipId == t) {
            receiverDtoList.push(i)
          }
        })
      })
      data.receiverDtoList = receiverDtoList
      console.log('🤡 ~ data:', data)
      // data.roleId = data.roleId.toString()
      // data.shipId = data.shipId.toString()
      // data.shipName = data.$shipId.split(' | ').toString()
      const _submit = async (comment) => {
        try {
          let reqMethod = this.id ? updateReply : addTextMsg
          console.log('🤡 ~ this.id:', this.id)
          await reqMethod(data)
          this.$message.success(`${comment}短报文成功`)
          this.$emit('ok')
          this.handleClose()
          done()
        } catch (error) {
          done()
        }
      }
      if (this.type === 'SAVE') {
        data.sendStatus = '0'
        this.$refs.addForm.validate((valid, done, result) => {
          done && done()
          if (valid) {
            _submit('暂存')
          } else {
            scrollToEl.call(this, this.$refs.addForm, result)
          }
        })
      } else {
        data.sendStatus = '1'
        this.$refs.addForm.validate((valid, done, result) => {
          done && done()
          if (valid) {
            _submit('发送')
          } else {
            scrollToEl.call(this, this.$refs.addForm, result)
          }
        })
      }
    }
  }
}
</script>
<style lang="scss" scoped></style>
